import { Controller, Inject, Get, Query, Body, Post, Put, Param, Del, Files, createMiddleware } from '@midwayjs/core';
import { UserService } from "@service/system/user.service";
import { DeptService } from "@service/system/dept.service";
import { ListUserDTO, CreateUserDTO, UpdateUserDTO, UpdatePwdDto, ResetPwdDto, ChangeStatusDto, UpdateProfileDto, UpdateAuthRoleDTO } from "@dto/system/user.dto";
import { Auth } from "@decorator/auth.decorator";
import { Log, BusinessType } from "@decorator/log.decorator";
import { UploadMiddleware, UploadFileInfo, UploadStreamFileInfo } from '@midwayjs/busboy';

@Controller('/system/user')
export class UserController {
  @Inject()
  userService: UserService;

  @Inject()
  deptService: DeptService;

  // 获取列表
  @Auth('system:user:list')
  @Get('/list')
  async list(@Query() queryParams: ListUserDTO) {
    return await this.userService.list(queryParams);
  }

  // 新增
  @Auth('system:user:add')
  @Log({ title: '用户管理', businessType: BusinessType.ADD })
  @Post('/')
  async create(@Body() user: CreateUserDTO) {
    return await this.userService.create(user);
  }

  // 删除，此接口仅解析数字类型，如果为其他值，不走此接口
  @Auth('system:user:remove')
  @Log({ title: '用户管理', businessType: BusinessType.REMOVE })
  @Del('/:userId?')
  async delete(@Param('userId') userId: string) {
    return await this.userService.delete(userId);
  }

  // 修改
  @Auth('system:user:edit')
  @Log({ title: '用户管理', businessType: BusinessType.EDIT })
  @Put('/')
  async update(@Body() user: UpdateUserDTO) {
    return await this.userService.update(user);
  }

  // 获取详情 userId非必填，因为新增时会调用无参的该接口
  @Auth('system:user:query')
  @Get('/:userId?')
  async get(@Param('userId') userId: number | undefined) {
    return await this.userService.detail(userId);
  }

  // 导出
  @Auth('system:user:export')
  @Log({ title: '用户管理', businessType: BusinessType.EXPORT })
  @Post('/export')
  async export(@Body() queryParams: ListUserDTO) {
    return await this.userService.export(queryParams);
  }

  // 下载导入模板
  @Post('/importTemplate')
  async importTemplate() {
    return await this.userService.importTemplate();
  }

  // 导入模板
  @Auth('system:user:import')
  @Log({ title: '用户管理', businessType: BusinessType.IMPORT })
  @Post('/importData', {
    middleware: [ createMiddleware(UploadMiddleware, { mode: 'stream' }) ]
  })
  async importData(
    @Files() files: UploadStreamFileInfo,
    @Query('updateSupport') updateSupport: boolean
  ) {
    return await this.userService.importData({
      files,
      updateSupport
    });
  }

  // 部门树状结构
  @Get('/deptTree')
  async deptTree() {
    return await this.deptService.deptTree();
  }

  // 列表页 -  重置密码
  @Auth('system:user:resetPwd')
  @Log({ title: '用户管理', businessType: BusinessType.EDIT })
  @Put('/resetPwd')
  async resetPwd(@Body() body: ResetPwdDto) {
    return await this.userService.resetPsw(body);
  }

  // 列表页 - 修改用户状态
  @Log({ title: '个人信息', businessType: BusinessType.EDIT })
  @Put('/changeStatus')
  async changeStatus(@Body() body: ChangeStatusDto) {
    return await this.userService.changeStatus(body);
  }

  // 列表页 - 分配角色详情及列表
  @Get('/authRole/:userId')
  async authRoleList(@Param('userId') userId: number) {
    return await this.userService.authRoleList(userId);
  }

  // 分配角色 提交更新
  @Put('/authRole')
  async authRoleUpdate(@Query() body: UpdateAuthRoleDTO) {
    return await this.userService.authRoleUpdate(body);
  }

  // 个人中心 - 获取个人信息
  @Get('/profile')
  async profile() {
    return await this.userService.profile();
  }

  // 个人中心 - 修改个人信息
  @Log({ title: '个人信息', businessType: BusinessType.EDIT })
  @Put('/profile')
  async updateProfile(@Body() body: UpdateProfileDto) {
    return await this.userService.updateProfile(body);
  }

  // 个人中心 - 修改密码
  @Log({ title: '个人信息', businessType: BusinessType.EDIT })
  @Put('/profile/updatePwd')
  async updatePwd(@Body() updatePwd: UpdatePwdDto) {
    return await this.userService.updatePwd(updatePwd);
  }

  // 上传头像
  @Log({ title: '个人信息', businessType: BusinessType.EDIT })
  @Post('/profile/avatar', { middleware: [UploadMiddleware] })
  async profileAvatar(@Files() files: UploadFileInfo) {
    return await this.userService.profileAvatar(files);
  }
}
